<?php defined('IN_NWIDC') or exit('Access Invalid!'); ?>

<div class="tabmenu">
	<?php include template('layout/submenu'); ?>
</div>

<form method="get" action="index.php" target="_self">
	<table class="search-form">
		<input type="hidden" name="act" value="store_order">
		<input type="hidden" name="op" value="tonglian_list">

		<tbody><tr>
			<td>&nbsp;
				姓名：
				<input type="text" class="text w150" name='name' value="<?php echo $_GET['name']?>">
				<label class="submit-border">
					<input type="submit" class="submit" value="搜索">
				</label>
			</td>

		</tr>
		</tbody></table>
</form>

<?php
	function _addItem(){
		if($_GET['method'] == 'add'){
			$name = trim($_GET['name']);
			$idcard = trim($_GET['idcard']);
			$tonglian_card = trim($_GET['tonglian_card']);
			if(empty($name) || empty($idcard) || empty($tonglian_card)){
				return;
			}
			if(ml('tonglian_card')->find([
				'name' => $name,
				'idcard' => $idcard,
//				'tonglian_card' => $tonglian_card
			])){
				echo "<script>layer.msg('已经有这个人了！')</script>";
				return;
			}
			ml('tonglian_card')->insert([
				'name' => $name,
				'idcard' => $idcard,
				'tonglian_card' => $tonglian_card
			]);
//			echo "<script>layer.msg('添加成功！')</script>";
			$_GET['name'] = '';
			$_GET['method'] = '';
			header("location:index.php?" . http_build_query($_GET));
			exit;
		}
	}
	_addItem();

?>

<form method="get" action="index.php" target="_self">
	<table class="search-form">
		<input type="hidden" name="act" value="store_order">
		<input type="hidden" name="op" value="tonglian_list">
		<input type="hidden" name="method" value="add">

		<tbody><tr>
			<td>&nbsp;
				姓名：
				<input type="text" class="text w150" name='name' >
				身份证号：
				<input type="text" class="text w150" name='idcard' >
				卡号：
				<input type="text" class="text w150" name='tonglian_card' >

				<label class="submit-border">
					<input type="submit" class="submit add_new" value="新增">
				</label>
			</td>

		</tr>
		</tbody></table>
</form>

<form action="" method="get" style="padding-top: 10px;padding-bottom: 10px;">


</form>

<form method="get" action="index.php" target="_self">
	<table class="search-form ncsc-default-table order">
		<input type="hidden" name="act" value="store_order"/>
		<input type="hidden" name="op" value="index"/>
		<?php if ($_GET['state_type']) { ?>
			<input type="hidden" name="state_type" value="<?php echo $_GET['state_type']; ?>"/>
		<?php } ?>

		<thead>

		<th>
			姓名
		</th>
		<th>
			身份证号
		</th>
		<th>
			卡号
		</th>
		<th>
			余额
		</th>
		<th>
			操作
		</th>
		</thead>
		<?php
			$m = Model()->table('tonglian_card');
			$where = [];
			if(!empty($_GET['name'])){
				$where['name'] = [
					'like',
					'%' . $_GET['name'] . '%'
				];
			}
			$list = $m
				->where($where)
//				->where([
//					'import_sn' => [
//						'neq',
//						''
//					],
//					'store_id'  => $_SESSION['store_id'],
//				])
//				->group('import_sn')
				->page(20)->select();
			foreach ($list as $item) {
				?>
				<tr>
				<td>
					<?php echo $item['name'] ?>
				</td>
				<td>
					<?php echo $item['idcard'] ?>
				</td>
					<td>
						<?php echo $item['tonglian_card'] ?>
					</td>
				<td>
					<?php echo $item['balance'] ?>
				</td>

				<td>
					<?php if(!empty($item['tonglian_card'])){?>
						<a href="javascript:;" class="jiebang" data-id="<?php echo $item['id'] ?>">解绑预付卡</a>
						<span style="padding-left: 10px;padding-right: 10px;">
							|
						</span>
						<a href="javascript:;" class="remove" data-id="<?php echo $item['id'] ?>">删除</a>
					<?php }?>
<!--					<a href="javascript:;" class="export" data-sn="--><?php //echo $item['sn'] ?><!--">导出</a>-->
<!--					<a href="javascript:;" target="_blank" style="display: none;">-->
<!--						下载-->
<!--					</a>-->
				</td>
				</tr>
				<?php
			}
		?>
		<script>
			$('[data-id].jiebang').click(function () {
				if(!confirm("确定要解绑吗？")){
					return;
				}
				var id = $(this).attr('data-id');
				var index = layer.load();
				var _this = $(this);
				$.post('/shop/?sin=SinSexcel&p=unbindCard&id=' + id, function (msg) {
					layer.close(index);
					if (msg.errcode != 0) {
						layer.msg("解绑失败！");
						return;
					}
					location.reload()
				}, 'json');
			});

			$('[data-id].remove').click(function () {
				if(!confirm("确定要删除吗？")){
					return;
				}
				var id = $(this).attr('data-id');
				var index = layer.load();
				var _this = $(this);
				$.post('/shop/?sin=SinSexcel&p=unlinkCard&id=' + id, function (msg) {
					layer.close(index);
					if (msg.errcode != 0) {
						layer.msg("删除失败！");
						return;
					}
					location.reload()
				}, 'json');
			});
		</script>
		<tr>
			<td colspan="100">
				<div class="pagination" style="padding-top: 20px;">
					<?php echo $m->showpage() ?>
				</div>
			</td>
		</tr>
	</table>
</form>
<script charset="utf-8" type="text/javascript"
        src="<?php echo RESOURCE_SITE_URL; ?>/js/jquery-ui/i18n/zh-CN.js"></script>
<link rel="stylesheet" type="text/css"
      href="<?php echo RESOURCE_SITE_URL; ?>/js/jquery-ui/themes/ui-lightness/jquery.ui.css"/>
<script type="text/javascript">
	$(function () {
		$('#query_start_date').datepicker({dateFormat: 'yy-mm-dd'});
		$('#query_end_date').datepicker({dateFormat: 'yy-mm-dd'});
		$('.checkall_s').click(function () {
			var if_check = $(this).attr('checked');
			$('.checkitem').each(function () {
				if (!this.disabled) {
					$(this).attr('checked', if_check);
				}
			});
			$('.checkall_s').attr('checked', if_check);
		});
		$('#skip_off').click(function () {
			url = location.href.replace(/&skip_off=\d*/g, '');
			window.location.href = url + '&skip_off=' + ($('#skip_off').attr('checked') ? '1' : '0');
		});
	});
</script> 
